cmake_minimum_required(VERSION 3.16)

project(StudentInfoSystem VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 选择要包含的QT模块
set(QT_MODULES
        Core Gui Qml Quick QuickControls2 Charts QuickControls2Impl
)
# 避免CLion识别错Qt的路径
set(CMAKE_PREFIX_PATH "E:/Tools/Develop/Embedded/Qt6.8.1/6.8.1/mingw_64")
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS ${QT_MODULES})
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS ${QT_MODULES})
# ================================== 辅助函数 =====================================
# 把需要的Qt模块转为相应的链接模块格式
function(format_qt_modules OUTPUT_VAR)
    set(formatted_list)
    foreach(module IN LISTS ARGN)
        list(APPEND formatted_list "Qt${QT_VERSION_MAJOR}::${module}")
    endforeach()
    set(${OUTPUT_VAR} ${formatted_list} PARENT_SCOPE)
endfunction()

# ================================== 资源文件 =====================================
# -------------- qml文件 ----------------
file(GLOB_RECURSE QML_SRCS "qml.qrc")

# -------------- cpp文件 ----------------
# Core层
file(GLOB_RECURSE CORE_SRCS
        "src/core/*.*"

)

# Data层
file(GLOB_RECURSE DATA_SRCS
        "src/data/*.*"

)

# Services层
file(GLOB_RECURSE SERVICES_SRCS
        "src/services/*.*"

)

# Tests层
file(GLOB_RECURSE TEST_SRCS
    "tests/*.*"
)

# ---------- 所有资源文件 ----------
include_directories(. src)

set(PROJECT_SOURCES
        ${CORE_SRCS}
        ${DATA_SRCS}
        ${SERVICES_SRCS}
        ${TEST_SRCS}
        ${QML_SRCS}
)

# =================================== 构建 ======================================
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(StudentInfoSystem
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET StudentInfoSystem APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(StudentInfoSystem SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(StudentInfoSystem
            ${PROJECT_SOURCES}
        )
    endif()
endif()

# 链接对应的Qt库
format_qt_modules(FORMATTED_MODULES ${QT_MODULES})
message(STATUS "${FORMATTED_MODULES}")
target_link_libraries(StudentInfoSystem PRIVATE ${FORMATTED_MODULES})

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.StudentInfoSystem)
endif()
set_target_properties(StudentInfoSystem PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS StudentInfoSystem
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(StudentInfoSystem)
endif()
